package com.geekcoding.geekoj.controller.admin;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geekcoding.common.dto.ApiPageResponse;
import com.geekcoding.common.dto.ApiResponse;
import com.geekcoding.common.dto.BasePageQuery;
import com.geekcoding.geekoj.entity.BaseEntity;
import com.geekcoding.geekoj.repository.BaseRepository;
import com.geekcoding.geekoj.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@RequiredArgsConstructor
public abstract class BaseAdminController<T extends BaseEntity, D, M extends BaseMapper<T>, R extends BaseRepository<T, D, M>> {
    protected final R repository;

    protected final UserService userService;


    public void saveOrUpdatePre(T req){

    }
    @PostMapping("/saveOrUpdate")
    public ApiResponse saveOrUpdate(@RequestBody T req) {
        saveOrUpdatePre(req);
        repository.saveOrUpdate(req);
        return ApiResponse.success(req);
    }

    @PostMapping("/page")
    public ApiResponse page(@RequestBody BasePageQuery query) {
        Page<T> tPage = repository.queryByPage(Page.of(query.getCurrent(), query.getPageSize()), null);
        return ApiResponse.success(tPage);
    }
}
